package main

import (
	"fmt"
	"net"
	"os"
	"io/ioutil"
	"time"
	"strconv"
)

func main(){
//	ParseIP("10.99.206.153")
//	TCPClientDemo("10.99.206.154:80")
//	TCPServerDemo("7070")
//	TCPServerDemo2("7070")
	TCPServerDemo3("7070")
}

func ParseIP(ipstr string){
//	ipstr:="10.99.206.153"
	ip:=net.ParseIP(ipstr)
	if ip==nil {
		fmt.Println("invalid ip address")
	}else{
		fmt.Println("resolve ip:",ip.String())
	}
}

func TCPClientDemo(service string){
	fmt.Println("----------TCPClientDemo----------")
	tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)
	checkError(err)
	conn,err:=net.DialTCP("tcp",nil,tcpAddr)
	checkError(err)
	_,err=conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
	checkError(err)
	result,err:=ioutil.ReadAll(conn)
	checkError(err)
	fmt.Println(string(result))
	os.Exit(0)
}

func checkError(err error){
	if err != nil {
		fmt.Fprintf(os.Stderr,"Fatail error:%s",err.Error())
		os.Exit(1)
	}
}

func TCPServerDemo(port string){
	fmt.Println("----------TCPServerDemo----------")
	service:=":"+port
	tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)
	checkError(err)
	listener,err:=net.ListenTCP("tcp",tcpAddr)
	checkError(err)
	for {
		conn,err:=listener.Accept()
		if err!=nil{
			continue
		}
		daytime:=time.Now().String()
		conn.Write([]byte(daytime))// don't care about return value
		conn.Close()// we're finished with this client
		fmt.Println("TCPServerDemo send ---------- ",daytime)
	}
	
}

func TCPServerDemo2(port string){
	fmt.Println("----------TCPServerDemo2----------")
	service:=":"+port
	tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)
	checkError(err)
	listener,err:=net.ListenTCP("tcp4",tcpAddr)
	checkError(err)
	for {
		conn,err:=listener.Accept()
		if err!=nil{
			continue
		}
		go handleClient(conn)
	}
}

func handleClient(conn net.Conn){
	defer conn.Close()
	daytime:=time.Now().String()
	fmt.Println("TCPServerDemo2 send ---------- ",daytime)
	conn.Write([]byte(daytime)) // don't care about return value
}

func TCPServerDemo3(port string){
	fmt.Println("----------TCPServerDemo3----------")
	service:=":"+port
	tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)
	checkError(err)
	listener,err:=net.ListenTCP("tcp4",tcpAddr)
	checkError(err)
	for {
		conn,err:=listener.Accept()
		if err!=nil{
			continue
		}
		go handleClientExt(conn)
	}
}

func handleClientExt(conn net.Conn){
	conn.SetReadDeadline(time.Now().Add(2*time.Minute))
	request:=make([]byte,128)
	defer conn.Close()
	for {
		read_len,err:=conn.Read(request)
		
		if err!=nil {
			fmt.Println(err)
			break
		}
		
		fmt.Println("read---------- ",read_len,string(request))
		
		if read_len==0 {
			break // connection already closed by client
		}else if string(request)=="timestamp" {
			daytime:=strconv.FormatInt(time.Now().Unix(),10)
			conn.Write([]byte(daytime))
			fmt.Println("write to client 1")
		}else {
			daytime := time.Now().String()
            conn.Write([]byte(daytime))
			fmt.Println("write to client 2")
		}
		
		request=make([]byte,128)// clear last read content
	}
	fmt.Println("-----end of handleClientExt-----")
}
